//
//  MSCustomerRecommenVC.m
//  SocietySale
//
//  Created by Mike on 16/11/30.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSCustomerRecommenVC.h"
#import "MSCustomerRecommenTableVC.h"
#import "MSRecommenDetailVC.h"

@interface MSCustomerRecommenVC ()
@property (nonatomic, strong) MSSearchBar               *searchBar;
@property (nonatomic, strong) NSMutableArray            *dataArray;     //数据
@property (nonatomic, strong) MSCustomerRecommenTableVC *tableVC;
@property(nonatomic, strong)UIView   *noResultView;             //无结果显示的页面
@property(nonatomic, strong)UILabel  *tipLabel;
@end

@implementation MSCustomerRecommenVC

- (void)initNavigationBar{
    [super initNavigationBar];
    leftBarBtn.hidden = NO;
    titleLable.text = @"找人脉";
}

- (void)initUI{
    self.tableVC = [[MSCustomerRecommenTableVC alloc] initWithStyle:UITableViewStyleGrouped];
    self.tableVC.view.frame = CGRectMake(0, 60, SCREENFRAME.size.width, SCREEN_HEIGHT-KNavBarHeight-60);
    self.tableVC.enableRefresh = NO;
    self.tableVC.enableRequest = YES;
    self.tableVC.delegate = (id)self;
    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableVC.view];
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENFRAME.size.width, 60)];
    bgView.backgroundColor = kDefaultViewBackgroundColor;
    [self.contentView addSubview:bgView];
//    self.tableVC.tableView.tableHeaderView = bgView;
    
    self.searchBar = [[MSSearchBar alloc] initWithFrame:CGRectMake(0, 10, SCREENWIDTH, 40)];
    self.searchBar.placeholder = @"输入客户姓名或手机号";
    self.searchBar.cornerRadius = 2;
    self.searchBar.delegate = (id)self;
    self.searchBar.placeholderColor = kDarkGrayColor;
    self.searchBar.borderColor = kNormalGray;
    self.searchBar.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search"]];
    [bgView addSubview:self.searchBar];
    [self.searchBar becomeFirstResponder];
}

- (void)testDataFunc{
    MSDetailContactModel *model = [[MSDetailContactModel alloc] init];
    model.name = @"周长生";
    model.position = @"项目经理";
    model.company = @"上海电力有限公司";
    model.source = @"平台采集更好让他赫然";
    
//    _dataArray = [@[model,model,model,model,model,model,model] mutableCopy];
//    self.tableVC.dataArray = _dataArray;
    self.noResultView.hidden = NO;
    self.tableVC.view.backgroundColor=[UIColor clearColor];
}

#pragma mark - getData
- (void)updateViewController:(NSNotification *)note
{
    NSDictionary *responseDic = note.userInfo;
    if (responseDic[Receiver]==self.VctrID) {
        if (responseDic[@"RecommandList"]!=nil){
            if (responseDic[@"state"]!=nil){
                self.tableVC.totalPagesNum = [responseDic[@"totalPage"] intValue];
                if (self.tableVC.pageNum == 1) {
                    self.tableVC.dataArray = nil;
                }
                NSMutableArray *dataArrray = [[NSMutableArray alloc] initWithArray:self.tableVC.dataArray];
                [dataArrray addObjectsFromArray:responseDic[@"RecommandList"]];
                self.tableVC.dataArray = dataArrray;
                
                self.noResultView.hidden = self.tableVC.dataArray.count;
                if (self.noResultView.hidden) {
                    self.tableVC.view.backgroundColor=kDefaultViewBackgroundColor;
                }else{
                    self.tableVC.view.backgroundColor=[UIColor clearColor];
                }
            }
            else{
                self.tableVC.dataArray = self.tableVC.dataArray;
            }
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MSDetailContactModel *model = self.tableVC.dataArray[indexPath.row];
    MSRecommenDetailVC *vc = [[MSRecommenDetailVC alloc] init];
    vc.model = model;
    [rootNavigation pushViewController:vc animated:YES];
    
    [self.searchBar resignFirstResponder];
}

#pragma mark - UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if ([[searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0 || searchText.length<2) {//字符全是空格，不做操作
        self.tableVC.dataArray = nil;
    }
    else{//无空字符时，开始匹配数据
        self.tableVC.pageNum = 1;
        [self requestSearchResult:NO];
    }
}

#pragma mark DYTableViewProtocol
/** 上拉 */
- (void)pullTableView:(UITableView *)tableView isNextPage:(BOOL)isNextPage{
    self.tableVC.pageNum++;
    [self requestSearchResult:YES];
}

-(void)requestSearchResult:(BOOL)isPull
{
    UIView * hudView = isPull?nil:self.contentView;
    NSString *pageNumber = [NSString stringWithFormat:@"%d",self.tableVC.pageNum];
    NSDictionary *dict = @{
                           @"kw":self.searchBar.text,
                           @"pageNo":pageNumber,
                           @"pageSize":@"15",
                           };
    [netEngine requestRecommandListWithTarget:self.VctrID hudView:hudView keyword:self.searchBar.text params:dict];
}

#pragma mark - scrollviewBeginDragging
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.searchBar resignFirstResponder];
}

- (UIView *)noResultView
{
    if (!_noResultView)
    {
        _noResultView = [[UIView alloc] initWithFrame:self.tableVC.view.frame];
        [self.contentView insertSubview:_noResultView belowSubview:self.tableVC.view];
        _noResultView.backgroundColor = kDefaultViewBackgroundColor;
        UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake((_noResultView.width-101)/2.0, (_noResultView.height-180)/2.0, 101, 101)];
        icon.image = [UIImage imageNamed:@"message_xiaolian"];
        [_noResultView addSubview:icon];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 5;
        paragraphStyle.alignment = NSTextAlignmentCenter;
        
        self.tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, icon.bottom+10, _noResultView.width, 50)];
        self.tipLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
        self.tipLabel.textColor = [UIColor lightGrayColor];
        self.tipLabel.numberOfLines = 0;
        self.tipLabel.textAlignment = NSTextAlignmentCenter;
        self.tipLabel.text = @"暂无数据";
        [_noResultView addSubview:self.tipLabel];
        _noResultView.hidden = YES;
    }
    return _noResultView;
}

@end
